import {Component, OnInit} from '@angular/core';
import {NavController} from 'ionic-angular';
import {NewCreatPage} from "../newcreat/newcreat";
import {StoragePage} from "../storage/storage";
import {CollectService} from "./collect.service";
import {CjStatusPage} from "../cjStatus/cjStatus";


@Component({
    selector: 'collect',
    templateUrl: 'collect.html',
    providers: [CollectService]
})
export class CollectPage implements OnInit {
    hasStorage = true;
    items = [];

    constructor(public navCtrl: NavController,
                public collectService: CollectService,) {

    }

    ngOnInit() {
        this.collectService.collectList()
            .then(data => {
                console.log(data);
                if (data[0].resultStatus == 'success') {
                    this.items = data[0].infoData.items;
                }
            }, err => {
                console.log('错误', err);
            })
            .catch(fail => {
                console.log('异常', fail);
            });
    }

    ToNewcreat() {
        this.navCtrl.setRoot(NewCreatPage);
    }

    ToStoragePage() {
        this.navCtrl.setRoot(StoragePage);
    }

    Tcheck() {
        this.navCtrl.push(CjStatusPage);
    }

    //下拉刷新
    doRefresh(refresher) {
        console.log('Begin async operation', refresher);
        this.collectService.collectList()
            .then(data => {
                console.log(data);
                if (data[0].resultStatus == 'success') {
                    this.items = data[0].infoData.items;
                    refresher.complete();
                }
            }, err => {
                console.log('错误', err);
            })
            .catch(fail => {
                console.log('异常', fail);
            });
    }

    //上拉加载更多
    doInfinite(infiniteScroll) {
        console.log('Begin async operation');
        this.collectService.collectList()
            .then(data => {
                console.log(data);
                if (data[0].resultStatus == 'success') {
                    this.items = this.items.concat(data[0].infoData.items);
                    infiniteScroll.complete();
                }
            }, err => {
                console.log('错误', err);
            })
            .catch(fail => {
                console.log('异常', fail);
            });
    }
}
